package com.fanhang.samples.spring.mvc.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "/log")
public class LogController {

	Logger logger = LoggerFactory.getLogger(this.getClass());

	@RequestMapping(value = "/index", method = { RequestMethod.GET })
	public ModelAndView index(HttpServletRequest rq) {
		logger.debug("debug!!!");
		logger.info("info!!!");
		logger.warn("warn!!!");
		logger.error("error!!!");

		String logPath = this.getClass().getResource("/").getPath();
		File dir = new File(logPath);
		File[] files = dir.listFiles();
		List<String> fileNames = new ArrayList<String>();
		for (File f : files) {
			fileNames.add(f.getName());
			System.out.println(f.getName());
		}

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("log/index");
		modelAndView.addObject("logFileNames", fileNames);
		return modelAndView;
	}
}
